package com.habdqn.studentmanager.util;

import java.util.Date;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.habdqn.studentmanager.bean.Dictionary;
import com.habdqn.studentmanager.bean.SysLog;
import com.habdqn.studentmanager.bean.Teacher;
import com.habdqn.studentmanager.constant.LogType;
import com.habdqn.studentmanager.mapper.SysLogMapper;

/**
 * @author Fan 系统日志类 2017年5月14日
 * 
 */
@Service
public class SystemLogger {

	private static Logger logger = Logger.getLogger(SystemLogger.class);

	/**
	 * 日志数据操作类
	 */
	@Resource
	private SysLogMapper sysLogMapper;

	/**
	 * 记录业务日志
	 * 
	 * @param type
	 *            日志类型
	 * @param content
	 *            日志内容
	 */
	public void record(LogType type, String content) {
		
		Teacher current = ProjectUtil.getCurrentUser();
		record(current, type, content);
	}
	
	/**
	 * 记录业务日志
	 * @param teacher 操作人
	 * @param type 日志类型
	 * @param content 日志内容
	 */
	public void record(Teacher teacher, LogType type, String content) {

		SysLog bean = new SysLog();
		bean.setTeacher(teacher);

		Dictionary dict = new Dictionary();
		dict.setValue(type.toInteger());
		bean.setType(dict);
		bean.setContent(content);
		bean.setCreateDate(new Date());
		
		String ip = ProjectUtil.getUserIp();
		bean.setExt3(ip);
		
		try {
			sysLogMapper.insert(bean);
		} catch (Exception e) {
			// 不影响正常流程
			logger.error("保存日志异常", e);
		}
	}
}
